package com.example.language

import android.os.Bundle
import android.widget.Button
import android.widget.ImageView
import android.widget.TextView
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.constraintlayout.widget.ConstraintSet

class MainActivity : AppCompatActivity() {
    private lateinit var button: Button
    private lateinit var textView: TextView
    private lateinit var imageView: ImageView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()

        // 创建 ConstraintLayout 作为根布局
        val constraintLayout = ConstraintLayout(this).apply {
            layoutParams = ConstraintLayout.LayoutParams(
                ConstraintLayout.LayoutParams.MATCH_PARENT,
                ConstraintLayout.LayoutParams.MATCH_PARENT
            )
        }

        // 创建 TextView
        textView = TextView(this).apply {
            id = TextView.generateViewId() // 生成唯一 ID
            layoutParams = ConstraintLayout.LayoutParams(
                ConstraintLayout.LayoutParams.WRAP_CONTENT,
                ConstraintLayout.LayoutParams.WRAP_CONTENT
            )
            text = getString(R.string.textView) // 设置从资源文件获取的文本
            textSize = 30f // 设置字体大小为 30sp
        }

        // 创建 Button
        button = Button(this).apply {
            id = Button.generateViewId() // 生成唯一 ID
            layoutParams = ConstraintLayout.LayoutParams(
                ConstraintLayout.LayoutParams.MATCH_PARENT,
                ConstraintLayout.LayoutParams.WRAP_CONTENT // 高度为 WRAP_CONTENT
            )
            text = getString(R.string.button) // 设置从资源文件获取的文本

            // 设置按钮点击事件
            setOnClickListener {
                textView.text = getString(R.string.clicked) // 点击后更新文本视图
            }
        }

        // 创建 ImageView
        imageView = ImageView(this).apply {
            id = ImageView.generateViewId() // 生成唯一 ID
            layoutParams = ConstraintLayout.LayoutParams(
                272,
                312
            )
            setImageResource(R.drawable.flag) // 设置图片资源
        }

        // 将 TextView、Button 和 ImageView 添加到 ConstraintLayout
        constraintLayout.addView(imageView)
        constraintLayout.addView(textView)
        constraintLayout.addView(button)

        // 创建并配置 ConstraintSet
        val constraintSet = ConstraintSet()
        constraintSet.clone(constraintLayout)

        // 设置 ImageView 的约束
        constraintSet.connect(imageView.id, ConstraintSet.TOP, constraintLayout.id, ConstraintSet.TOP, 200)
        constraintSet.connect(imageView.id, ConstraintSet.START, constraintLayout.id, ConstraintSet.START, 0)
        constraintSet.connect(imageView.id, ConstraintSet.END, constraintLayout.id, ConstraintSet.END, 0)
        constraintSet.constrainHeight(imageView.id, 1000) // 设置 ImageView 高度
        constraintSet.constrainWidth(imageView.id, 1300) // 设置 ImageView 宽度

        // 设置 TextView 的约束
        constraintSet.connect(textView.id, ConstraintSet.TOP, imageView.id, ConstraintSet.BOTTOM, 200) // 距离 ImageView 16dp
        constraintSet.connect(textView.id, ConstraintSet.START, constraintLayout.id, ConstraintSet.START, 0) // 左侧约束
        constraintSet.connect(textView.id, ConstraintSet.END, constraintLayout.id, ConstraintSet.END, 0) // 右侧约束

        // 设置 Button 的约束
        constraintSet.connect(button.id, ConstraintSet.TOP, textView.id, ConstraintSet.BOTTOM, 200) // 距离 TextView 16dp
        constraintSet.connect(button.id, ConstraintSet.START, constraintLayout.id, ConstraintSet.START, 0) // 左侧约束
        constraintSet.connect(button.id, ConstraintSet.END, constraintLayout.id, ConstraintSet.END, 0) // 右侧约束

        // 应用 ConstraintSet
        constraintSet.applyTo(constraintLayout)

        // 设置内容视图为 ConstraintLayout
        setContentView(constraintLayout)
    }
}